﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CS424_9pts.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            TextBox userNameTextBox = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("UserName");
            TextBox Phone = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Phone");
            TextBox City = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("City");
            TextBox Address = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Address");
            TextBox FullName = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("FullName");
            RadioButtonList gender = (RadioButtonList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Gender");             
            
            MembershipUser user = Membership.GetUser(userNameTextBox.Text);
            user.Comment = "Phone=" + Phone.Text + "&" +
                           "City=" + City.Text;
            Membership.UpdateUser(user);

            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

            SqlDataSource1.UpdateParameters.Add("City", City.Text);
            SqlDataSource1.UpdateParameters.Add("Address", Address.Text);
            SqlDataSource1.UpdateParameters.Add("Phone", Phone.Text);
            SqlDataSource1.UpdateParameters.Add("UserId", user.ProviderUserKey.ToString());
            SqlDataSource1.UpdateParameters.Add("Gender", gender.SelectedValue);
            SqlDataSource1.UpdateParameters.Add("FullName", FullName.Text);
            SqlDataSource1.Update();

            Session["UserName"] = user.UserName;
            Session["UserId"] = user.ProviderUserKey.ToString();

            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }
            Response.Redirect(continueUrl);
        }

    }
}
